package ym.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ym.mapper.PermissionMapper;
import ym.pojo.Permission;

import java.util.List;


@DubboService
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RolePermissionService rolePermissionService;

    @Override
    public void add(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateById(permission);
    }

    @Override
    public void delete(Integer id) {
        permissionMapper.deleteById(id);
        //删除角色_权限中间表的数据
        rolePermissionService.deleteByPermissionId(id);
    }

    @Override
    public Permission findById(Integer id) {
        return permissionMapper.selectById(id);
    }

    @Override
    public List<Permission> findAll() {
        return permissionMapper.selectList(null);
    }

    @Override
    public Page<Permission> search(String search, int page, int size) {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        if (search != null) {
            queryWrapper.like("permissionName", search);
        }
        return permissionMapper.selectPage(new Page(page, size), queryWrapper);
    }
}
